package cn.crm.web.controller.business;

import cn.crm.common.annotation.Log;
import cn.crm.common.core.controller.BaseController;
import cn.crm.common.core.domain.AjaxResult;
import cn.crm.common.core.page.TableDataInfo;
import cn.crm.common.enums.BusinessType;
import cn.crm.system.domain.pojo.dto.BusinessDto;
import cn.crm.system.domain.pojo.dto.ContactsDto;
import cn.crm.system.domain.pojo.vo.ContactsVo;
import cn.crm.system.service.IContactsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/contacts")
@Api(tags = "业务联系人模块模块")
public class ContactsController  extends BaseController {


    @Autowired
    private IContactsService iContactsService;

    @GetMapping("/list")
    @ApiOperation("查询联系人资料")
    @Log(title = "联系人管理", businessType = BusinessType.INSERT)
    public TableDataInfo ListContacts(Long  businessId  ) {
        log.debug("开始处理查询联系人资料的接口");
        startPage();
        List<ContactsVo> contactsVos = iContactsService.selectContactsbyBusinessid(businessId);
        return  getDataTable(contactsVos) ;

    }
    @Log(title = "联系人管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @ApiOperation("删除联系人资料")
    public AjaxResult delete(@PathVariable Long[] id){
        log.debug("开始删除联系人的接口");
        int deletecontacts = iContactsService.deletecontacts(id);
        if (deletecontacts < 1){
            return AjaxResult.error("删除客户资料失败");
        }

        return AjaxResult.success("删除客户资料成功");
    }


    @Log(title = "联系人管理", businessType = BusinessType.DELETE)
    @PostMapping("/add")
    @ApiOperation("添加联系人资料")
    public AjaxResult addContacts(@RequestBody ContactsDto contactsDto){
        log.debug("开始新增联系人的接口");
        int i = iContactsService.insertContacts(contactsDto);
        if (i<1){
            return AjaxResult.error("添加联系人失败");
        }

        return AjaxResult.success("添加联系人成功");
    }

    @Log(title = "联系人管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    @ApiOperation("修改客户资料")
    public AjaxResult update( @RequestBody ContactsDto contactsDto){
        log.debug("开始修改联系人资料的接口");
        int i =    iContactsService.updateContacts(contactsDto);
        if (i < 1){
            return AjaxResult.error("修改联系人失败");
        }
        return AjaxResult.success("修改联系人资料成功");
    }

}
